package com.ruoyi.core.config.pay;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.core.constant.WxConfigType;
import com.ruoyi.core.domain.vo.ConfigVo;
import com.ruoyi.core.service.IConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Binary Wang
 */
@Slf4j
@Configuration
@ConditionalOnClass(WxPayService.class)
@AllArgsConstructor
public class WxPayConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public static WxPayService wxPayService() {


        log.info("加载微信支付配置");
        // 代码里 getConfigs()处报错的同学，请注意仔细阅读项目说明，你的IDE需要引入lombok插件！！！！
        List<ConfigVo> payConfigs = SpringUtils.getBean(IConfigService.class).listWxPayConfig();
        if (payConfigs == null || payConfigs.size() == 0) {
            log.error("未找到微信支付配置类");
            return new WxPayServiceImpl();
        }
        WxPayService wxPayService = new WxPayServiceImpl();
//        wxPayService.setMultiConfig(payConfigs.stream().map(a -> {
//            WxPayConfig payConfig = new WxPayConfig();
//            payConfig.setAppId(StringUtils.trimToNull(a.getAppid()));
//            payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
//            payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
//            payConfig.setKeyPath(StringUtils.trimToNull(a.getLp12CertPath()));
//            payConfig.setUseSandboxEnv(false);
//            return payConfig;
//        }).collect(Collectors.toMap(WxPayConfig::getMchId, a -> a, (o, n) -> o)));
//        Map<String, WxPayConfig> configMap1 = payConfigs.stream().map(a -> {
//            WxPayConfig payConfig = new WxPayConfig();
//            payConfig.setAppId(StringUtils.trimToNull(a.getAppid()));
//            payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
//            payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
//            payConfig.setKeyPath(StringUtils.trimToNull(a.getLp12CertPath()));
//            payConfig.setUseSandboxEnv(false);
//            return payConfig;
//        }).collect(Collectors.toMap(WxPayConfig::getAppId, a -> a, (o, n) -> o));
//        Map<String, WxPayConfig> configMap2 = payConfigs.stream().map(a -> {
//            WxPayConfig payConfig = new WxPayConfig();
//            payConfig.setAppId(StringUtils.trimToNull(a.getAppid()));
//            payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
//            payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
//            payConfig.setKeyPath(StringUtils.trimToNull(a.getLp12CertPath()));
//            payConfig.setUseSandboxEnv(false);
//            return payConfig;
//        }).collect(Collectors.toMap(WxPayConfig::getMchId, a -> a, (o, n) -> o));
//        configMap1.putAll(configMap2);
//        Map<String, WxPayConfig> configMap = configMap1;
//        wxPayService.setMultiConfig(configMap);

        Map<String, WxPayConfig> configMap = new HashMap<>();
        payConfigs.forEach(a -> {
            WxPayConfig payConfig = new WxPayConfig();
            payConfig.setAppId(StringUtils.trimToNull(a.getAppid()));
            payConfig.setMchId(StringUtils.trimToNull(a.getMchId()));
            payConfig.setMchKey(StringUtils.trimToNull(a.getMchKey()));
            payConfig.setKeyPath(StringUtils.trimToNull(a.getLp12CertPath()));
            payConfig.setUseSandboxEnv(false);
            configMap.put(payConfig.getAppId(), payConfig);
            configMap.put(payConfig.getMchId(), payConfig); // 同一个mchId有可能有多个appid
        });
        wxPayService.setMultiConfig(configMap);
        return wxPayService;
    }


}
